﻿package tools;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class StringDemo {
	public static void main(String[] args) {
		String str1 = "xxxzzzddd2";
		String str2 = "xxxzzzddD1";
		System.out.println("str1.length=" + str1.length());
		System.out.println("str2.length=" + str2.length());
		boolean b1 = str1.regionMatches(1, str2, 1, 8);
		// 忽略大小写
		boolean b2 = str1.regionMatches(true, 1, str2, 1, 8);
		System.out.println("b1=" + b1);
		System.out.println("b2=" + b2);
		System.out.println(str1.startsWith("z"));// F
		System.out.println(str2.endsWith("1"));// T
		System.out.println(str2.startsWith("1", 9));// T
		char c = str1.charAt(9);
		System.out.println("C=" + c);
		// 取str1的1到8位字的符串从ch字符数组的第十位开始放入
		char[] ch = new char[20];
		str1.getChars(1, 8, ch, 10);
		System.out.println(ch);
		System.out.println(str1.indexOf('x'));// 0
		System.out.println(str1.lastIndexOf('x'));// 2
		System.out.println(str1.indexOf("xx"));// 0
		System.out.println(str1.lastIndexOf("xxz"));// 1
		System.out.println(str1.substring(2, 4));// xz
		String sentence = "    This is a man,that is too.";
		String sub = " is ";
		String rep = " was ";
		String res = sentence;
		int i = sentence.indexOf(" is ");
		System.out.println("i=" + i);
		while (i != -1) {
			res = res.substring(0, i) + rep + res.substring(i + sub.length());
			System.out.println("res=" + res);
			i = res.indexOf(" is ");
		}
		System.out.println("sentence=" + sentence);
		System.out.println("res=" + res);
		// 直接用replace()方法替代
		System.out.println("replace:" + sentence.replace(sub, rep));
		// 去除头尾空格
		System.out.println("trim:" + sentence.trim());
		System.out.println("UPPER:" + sentence.toUpperCase());
		System.out.println("LOWER:" + sentence.toLowerCase());
		//UTF-8转ISO-8859-1
		try {
			String strgbk=new String("asd");
			byte[] byteutf;
			byteutf = strgbk.getBytes("UTF-8");
			System.out.println(new String(byteutf,"ISO8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//编码解码
		try {
			String strgbk=new String("好大");
			String SE=URLEncoder.encode(strgbk, "UTF-8");
			String SD=URLDecoder.decode(new String(SE.getBytes("ISO8859-1"),"UTF-8"),"UTF-8");
			System.out.println("SE="+SE+"\nSD="+SD);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}

}
